#include "textedit.h"
#include "ui_textedit.h"

#include <QMessageBox>
#include <QPainter>
#include <QTimer>

#define BACKGROUND_COLOR QColor(255, 255, 255, 240)
#define RADIUS 15

TextEdit::TextEdit(QString absolute_path, int center_x, QWidget *parent)
    : QWidget(parent), ui(new Ui::TextEdit), file(absolute_path), something_changed(false), pointer_x(center_x)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_TranslucentBackground);  // 设置窗口背景透明
    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);  // 让窗口无边框

    qDebug() << "TextEdit创建了";
    setFixedSize(width_, height_);

    absolute_path.replace('\\', '/');
    filename_ = absolute_path.section('/', -1);
    ui->label_filename->setText(filename_);

    connect(ui->btn_close, &QPushButton::clicked, this, [this]()
    {
        save_file();

        close();
        deleteLater();
    });

    bool ret = file.open(QIODevice::ReadOnly);
    if (! ret)
    {
        QMessageBox::warning(this, "警告", "文件打开失败！");
        close();
        deleteLater();
    }
    else
    {
        auto data = file.readAll();
        file.close();
        ui->pte_main->setPlainText(data);
    }

    // 有文字改变
    connect(ui->pte_main, &QPlainTextEdit::textChanged, this, [this]()
    {
        something_changed = true;
        ui->label_filename->setText(filename_ + "*");
    });

    set_auto_save();
}

void TextEdit::set_auto_save()
{
    // 每隔1秒检查是否有文字改变，有则自动保存
    QTimer* timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [this]()
    {
        if (something_changed)
        {
            save_file();
        }
    });
    timer->start(1000);
}

void TextEdit::save_file()
{
    bool ret = file.open(QIODevice::WriteOnly);
    if (! ret)
        QMessageBox::warning(this, "警告", "文件写入失败！");
    else
    {
        file.write(ui->pte_main->toPlainText().toUtf8());
        file.close();
        ui->label_filename->setText(filename_);
        something_changed = false;
    }
}

void TextEdit::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 设置抗锯齿

    QPen pen(BACKGROUND_COLOR);
    pen.setWidth(0);
    painter.setPen(pen);

    QBrush brush(BACKGROUND_COLOR);
    painter.setBrush(brush);

    QRectF rect(0, 0, this->width(), this->height() - 10);
    painter.drawRoundedRect(rect, RADIUS, RADIUS);  // 设置矩形的圆角

    // 画窗口下方的三角形
    const QPointF points[3] =
    {
        QPointF(pointer_x - 10, height() - 10),
        QPointF(pointer_x + 10, height() - 10),
        QPointF(pointer_x, height())
    };

    painter.drawConvexPolygon(points, 3);
}

void TextEdit::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Control:
        pressed_ctrl = true;
        break;
    case Qt::Key_S:
        if (pressed_ctrl)
            save_file();
        break;
    }
}

void TextEdit::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Control:
        pressed_ctrl = false;
        break;
    }
}

TextEdit::~TextEdit()
{
    qDebug() << "TextEdit销毁了";
    delete ui;
}
